<?php
// This file contains functions used by the admin interface
// for the Sensual Clips shopping cart.

function display_category_form($category = '') {
// This displays the category form.
// This form can be used for inserting or editing categories.
// To insert, don't pass any parameters.	This will set $edit
// to false, and the form will go to insert_category.php.
// To update, pass an array containing a category.	The
// form will contain the old data and point to update_category.php.
// It will also add a "Delete category" button.

	// if passed an existing category, proceed in "edit mode"
	$edit = is_array($category);

	// most of the form is in plain HTML with some
	// optional PHP bits throughout
?>
	<form method="post"
			action="<?php echo $edit ? 'edit_category.php' : 'insert_category.php'; ?>">
	<table border="0" bgcolor="#cccccc">
	<tr>
		<td>Category Name:</td>
		<td><input type="text" name="catname" size="40" maxlength="40"
					value="<?php echo $edit ? $category['catname'] : ''; ?>" /></td>
	 </tr>
	<tr>
		<td <?php if (!$edit) { echo "colspan=2";} ?> align="center">
			<?php
				 if ($edit) {
						echo "<input type=\"hidden\" name=\"catid\" value=\"".$category['catid']."\" />";
				 }
			?>
			<input type="submit"
			 value="<?php echo $edit ? 'Update' : 'Add'; ?> Category" /></form>
		 </td>
		 <?php
				if ($edit) {
					//allow deletion of existing categories
					echo "<td>
								<form method=\"post\" action=\"delete_category.php\">
								<input type=\"hidden\" name=\"catid\" value=\"".$category['catid']."\" />
								<input type=\"submit\" value=\"Delete category\" />
								</form></td>";
			 }
		 ?>
	</tr>
	</table>
<?php
}

function display_paperclip_form($paperclip = '') {
// This displays the paper clip form.
// It is very similar to the category form.
// This form can be used for inserting or editing paper clips.
// To insert, don't pass any parameters.	This will set $edit
// to false, and the form will go to insert_paperclip.php.
// To update, pass an array containing a paper clip.	The
// form will be displayed with the old data and point to update_paperclip.php.
// It will also add a "Delete paper clip" button.


	// if passed an existing paper clip, proceed in "edit mode"
	$edit = is_array($paperclip);

	// most of the form is in plain HTML with some
	// optional PHP bits throughout
?>
	<form method="post"
				action="<?php echo $edit ? 'edit_paperclip.php' : 'insert_paperclip.php';?>">
	<table border="0" bgcolor="#cccccc">
	<tr>
		<td>SKU:</td>
		<td><input type="text" name="sku"
				 value="<?php echo $edit ? $paperclip['sku'] : ''; ?>" /></td>
	</tr>
	<tr>
		<td>Paper Clip Name:</td>
		<td><input type="text" name="clipname"
				 value="<?php echo $edit ? $paperclip['clipname'] : ''; ?>" /></td>
	</tr>
	<tr>
		<td>Paper Clip Designer:</td>
		<td><input type="text" name="designer"
				 value="<?php echo $edit ? $paperclip['designer'] : ''; ?>" /></td>
	 </tr>
	 <tr>
			<td>Category:</td>
			<td><select name="catid">
			<?php
					// list of possible categories comes from database
					$cat_array=get_categories();
					foreach ($cat_array as $thiscat) {
							 echo "<option value=\"".$thiscat['catid']."\"";
							 // if existing paper clip, put in current catgory
							 if (($edit) && ($thiscat['catid'] == $paperclip['catid'])) {
									 echo " selected";
							 }
							 echo ">".$thiscat['catname']."</option>";
					}
					?>
					</select>
				</td>
	 </tr>
	 <tr>
		<td>Price:</td>
		<td><input type="text" name="price"
							 value="<?php echo $edit ? $paperclip['price'] : ''; ?>" /></td>
	 </tr>
	 <tr>
		 <td>Description:</td>
		 <td><textarea rows="3" cols="50"
					name="description"><?php echo $edit ? $paperclip['description'] : ''; ?></textarea></td>
		</tr>
		<tr>
			<td <?php if (!$edit) { echo "colspan=2"; }?> align="center">
				 <?php
						if ($edit)
						 // we need the old sku to find paper clip in database
						 // if the sku is being updated
						 echo "<input type=\"hidden\" name=\"oldsku\"
										value=\"".$paperclip['sku']."\" />";
				 ?>
				<input type="submit"
							 value="<?php echo $edit ? 'Update' : 'Add'; ?> Paper Clip" />
				</form></td>
				<?php
					 if ($edit) {
						 echo "<td>
									 <form method=\"post\" action=\"delete_paperclip.php\">
									 <input type=\"hidden\" name=\"sku\"
										value=\"".$paperclip['sku']."\" />
									 <input type=\"submit\" value=\"Delete paper clip\"/>
									 </form></td>";
						}
					?>
				 </td>
			</tr>
	</table>
	</form>
<?php
}

function display_password_form() {
// displays html change password form
?>
	 <br />
	 <form action="change_password.php" method="post">
	 <table width="250" cellpadding="2" cellspacing="0" bgcolor="#cccccc">
	 <tr><td>Old password:</td>
			 <td><input type="password" name="old_passwd" size="16" maxlength="16" /></td>
	 </tr>
	 <tr><td>New password:</td>
			 <td><input type="password" name="new_passwd" size="16" maxlength="16" /></td>
	 </tr>
	 <tr><td>Repeat new password:</td>
			 <td><input type="password" name="new_passwd2" size="16" maxlength="16" /></td>
	 </tr>
	 <tr><td colspan=2 align="center"><input type="submit" value="Change password">
	 </td></tr>
	 </table>
	 <br />
<?php
}

function insert_category($catname) {
// inserts a new category into the database

	 $conn = db_connect();

	 // check category does not already exist
	 $query = "select *
						 from categories
						 where catname='".$catname."'";
	 $result = $conn->query($query);
	 if ((!$result) || ($result->num_rows!=0)) {
		 return false;
	 }

	 // insert new category
	 $query = "insert into categories values
						('', '".$catname."')";
	 $result = $conn->query($query);
	 if (!$result) {
		 return false;
	 } else {
		 return true;
	 }
}

function insert_paperclip($sku, $clipname, $designer, $catid, $price, $description) {
// insert a new paper clip into the database

	 $conn = db_connect();

	 // check paper clip does not already exist
	 $query = "select *
						 from paperclips
						 where sku='".$sku."'";

	 $result = $conn->query($query);
	 if ((!$result) || ($result->num_rows!=0)) {
		 return false;
	 }

	 // insert new paper clip
	 $query = "insert into paperclips values
						('".$sku."', '".$designer."', '".$clipname."',
						 '".$catid."', '".$price."', '".$description."')";

	 $result = $conn->query($query);
	 if (!$result) {
		 return false;
	 } else {
		 return true;
	 }
}

function update_category($catid, $catname) {
// change the name of category with catid in the database

	 $conn = db_connect();

	 $query = "update categories
						 set catname='".$catname."'
						 where catid='".$catid."'";
	 $result = @$conn->query($query);
	 if (!$result) {
		 return false;
	 } else {
		 return true;
	 }
}

function update_paperclip($oldsku, $sku, $clipname, $designer, $catid,
										 $price, $description) {
// change details of paper clip stored under $oldsku in
// the database to new details in arguments

	 $conn = db_connect();

	 $query = "update paperclips
						 set sku= '".$sku."',
						 clipname = '".$clipname."',
						 designer = '".$designer."',
						 catid = '".$catid."',
						 price = '".$price."',
						 description = '".$description."'
						 where sku = '".$oldsku."'";

	 $result = @$conn->query($query);
	 if (!$result) {
		 return false;
	 } else {
		 return true;
	 }
}

function delete_category($catid) {
// Remove the category identified by catid from the db
// If there are paper clips in the category, it will not
// be removed and the function will return false.

	 $conn = db_connect();

	 // check if there are any paper clips in category
	 // to avoid deletion anomalies
	 $query = "select *
						 from paperclips
						 where catid='".$catid."'";

	 $result = @$conn->query($query);
	 if ((!$result) || (@$result->num_rows > 0)) {
		 return false;
	 }

	 $query = "delete from categories
						 where catid='".$catid."'";
	 $result = @$conn->query($query);
	 if (!$result) {
		 return false;
	 } else {
		 return true;
	 }
}


function delete_paperclip($sku) {
// Deletes the paper clip identified by $sku from the database.

	 $conn = db_connect();

	 $query = "delete from paperclips
						 where sku='".$sku."'";
	 $result = @$conn->query($query);
	 if (!$result) {
		 return false;
	 } else {
		 return true;
	 }
}

?>
